<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class cms_controller extends APP_Controller {

    public $session_user_data;
    public $user_id;
    public $admin_id;
    public $site_close = false;

    function __construct() {
        parent::__construct();

        $this->admin_id = $this->session->userdata('admin_ID');
        $this->valid_close_site();
        $this->session_user_data = array(
            'user_id' => $this->session->userdata('user_ID'),
            'user_name' => $this->session->userdata('user_NAME'),
            'user_email' => $this->session->userdata('user_EMAIL'),
            'user_password' => $this->session->userdata('user_PASSWORD'),
            'user_lastlogin' => $this->session->userdata('user_LASTLOGIN'),
            'user_confirm' => $this->session->userdata('user_CONFIRM'),
            'user_active' =>$this->session->userdata('user_ACTIVE'),
            'logged_in' => $this->session->userdata('user_logged_in')
        );
        $this->user_id = $this->session->userdata('user_ID');
        $this->allow_view_user();
    }

    public function allow_view_user() {

        $array_alrady_login = array(
            'user/login',
            'user/register',
            'user/forgetpassword',
            'user/reset_password'
        );

        $array_must_confirm = array(
            // ============ addressbook =====
            'addressbook',
            'addressbook/index',
            'addressbook/add',
            'addressbook/edit',
            'addressbook/delete',
            'addressbook/default',
            // ================ cart ==============
            'cart/checkout',
            'cart/update',
            //============ cases ==============
            'cases',
            'cases/index',
            'cases/add',
            'cases/close',
            'cases/add_replay',
            'cases/get_one_replay',
            'cases/get_history',
            'cases/add_history',
            'cases/view',
            // =========== invoicies ========
            'invoicies',
            'invoicies/index',
            'invoicies/show',
            'invoicies/pay',
            'invoicies/paytypes',
            'invoicies/minum_than_pay',
            'invoicies/cancel',
            // ============ mywallet =====
            'mywallet',
            'mywallet/add',
            'mywallet/withdraw',
            // ============ private_messages =====
            'private_messages',
            'private_messages/index',
            'private_messages/send',
            'private_messages/view',
            'private_messages/replay',
            'private_messages/default',
            // ============ Products ==========
            'products/add',
            'products/edit',
            // ============ store =====
            'store/edit',
            'store/create',
            'store/subscribe',
            'store/unsubscribe',
            'store/like',
            'store/dislike',
            'store/subscribers',
            'store/deals',
            'store/showdeal',
            'store/shipingdetails',
            'store/upgrade',
            // ============ account ========
            'user/stores',
            'user/purshases',
            'user/sales',
            'user/showdeal',
            'user/shipingdetails',
            'user/delivers',
            // =========== user_sms ============
            'user_sms/index',
            'user_sms/send',
            'user_sms/view',
            'user_sms/cancel',
            
            // ========== advertise =============
            'advertise',
            'advertise/index',
            'advertise/add',
        );

        $array_must_login = array(
            // =========== invoicies ========
            'invoicies',
            'invoicies/index',
            'invoicies/show',
            'invoicies/pay',
            'invoicies/paytypes',
            'invoicies/minum_than_pay',
            'invoicies/cancel',
            // ============ addressbook =====
            'addressbook',
            'addressbook/index',
            'addressbook/add',
            'addressbook/edit',
            'addressbook/delete',
            'addressbook/default',
            // ============ orders =====
            'orders',
            'orders/index',
            'orders/histroy/',
            'orders/edit',
            'orders/delete',
            'orders/forshiping',
            'box',
            'bill',
            'bill/view/',
            // ============ private_messages =====
            'private_messages',
            'private_messages/index',
            'private_messages/send',
            'private_messages/view',
            'private_messages/replay',
            'private_messages/default',
            // ============ email =====
            'email_imprint',
            'email_imprint/valid',
            // ============ mywallet =====
            'mywallet',
            'mywallet/add',
            'mywallet/withdraw',
            // ============ account ========
            'account',
            'user/profile',
            'user/logout',
            'user/stores',
            'user/purshases',
            'user/sales',
            'user/edit',
            'user/showdeal',
            'user/shipingdetails',
            'user/delivers',
            // ============ store =====
            'store/edit',
            'store/create',
            'store/subscribe',
            'store/unsubscribe',
            'store/like',
            'store/dislike',
            'store/subscribers',
            'store/deals',
            'store/showdeal',
            'store/shipingdetails',
            'store/upgrade',
            // ============ Products ==========
            'products/add',
            'products/edit',
            // ================ cart ==============
            'cart/checkout',
            'cart/update',
            //============ cases ==============
            'cases',
            'cases/index',
            'cases/add',
            'cases/close',
            'cases/add_replay',
            'cases/get_one_replay',
            'cases/get_history',
            'cases/add_history',
            'cases/view',
            // =========== user_sms ============
            'user_sms/index',
            'user_sms/send',
            'user_sms/view',
            'user_sms/cancel',
            
            // ========== advertise =============
            'advertise',
            'advertise/index',
            'advertise/add',
        );

        $segment = trim($this->uri->segment(1) . '/' . $this->uri->segment(2), '/');
        $logged_in = $this->session->userdata('user_logged_in');
        $confirm = $this->session->userdata('user_ACTIVE');

        if ($logged_in == '1' && in_array($segment, $array_alrady_login)) {
            $this->themes->set_message(array('url' => '', 'type' => 'error', 'message' => 'message_allready_login_in'));
            return false;
        }

        if (empty($logged_in) && in_array($segment, $array_must_login)) {
            $this->themes->set_message(array('url' => 'user/login/', 'type' => 'error', 'message' => 'message_y_must_login'));
            return false;
        }

        if (!empty($logged_in) && $confirm == 'NO' && in_array($segment, $array_must_confirm)) {
            $this->themes->set_message(array('url' => 'user/profile/', 'type' => 'error', 'message' => 'message_y_must_confirm'));
            return false;
        }
    }

    public function valid_close_site() {
        $this->db->select('value');
        $close = $this->db->get_where('settings', array('code' => 'site_close'))->row()->value;
        $this->db->select('value');
        $reasone = $this->db->get_where('settings', array('code' => 'site_close_resone'))->row()->value;

        if ($close && !$this->admin_id) {
            $this->db->select('value');
            $sitename = $this->db->get_where('settings', array('code' => 'site_title'))->row()->value;
            $this->session->set_userdata('sitename', $sitename);
            $this->load->view($this->themes->Cms_themes() . '/message/close', array('message' => $reasone));
        } elseif ($close && $this->admin_id) {
            $this->site_close = true;
        }
    }

}
